package com.pt25.base.dep.redis;


import com.pt25.base.constants.BaseConstants;
import com.pt25.base.util.CollectionUtil;
import jakarta.annotation.Resource;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.Codec;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
class RedisConfig {
    @Resource
    private RedisProperties redisProperties;

    private static final Map<String, RedissonClient> initRedissonClientMap = new HashMap<>();

    @Bean
    Map<String, RedissonClient> redissonClientMap() {
        Map<String, RedissonClient> clientMap = new HashMap<>();
        if (CollectionUtil.isNotEmpty(redisProperties.getConfigs()) && redisProperties.checkConfigs()) {
            for (RedisProperties.RedissonConfig conf : redisProperties.getConfigs()) {
                String confStr = conf.toString();
                if (null != initRedissonClientMap.get(confStr)) {
                    RedissonClient redissonClient = initRedissonClientMap.get(confStr);
                    clientMap.put(conf.getName(), redissonClient);
                } else {
                    Config config = new Config();
                    Codec codec = initCodec();
                    if (null != codec) {
                        config.setCodec(codec);
                    }
                    if ("cluster".equals(conf.getMode())) {
                        config.useClusterServers()
                                .setPassword(conf.getPassword())
                                .addNodeAddress(conf.getNodes().toArray(new String[0]));

                    } else if ("master_slave".equals(conf.getMode())) {
                        config.useMasterSlaveServers()
                                .setPassword(conf.getPassword())
                                .setMasterAddress(conf.getMaster())
                                .addSlaveAddress(conf.getNodes().toArray(new String[0]));
                    } else if ("single".equals(conf.getMode())) {
                        config.useSingleServer()
                                .setPassword(conf.getPassword())
                                .setAddress(conf.getMaster());
                    } else {
                        throw new IllegalArgumentException("Unsupported Redisson mode: " + conf.getMode());
                    }
                    RedissonClient redissonClient = Redisson.create(config);
                    clientMap.put(conf.getName(), redissonClient);
                    initRedissonClientMap.put(conf.toString(), redissonClient);
                }
            }
            if (null != clientMap.get(redisProperties.getPrimary())) {
                clientMap.put(BaseConstants.DEFAULT, clientMap.get(redisProperties.getPrimary()));
            }
        }
        return clientMap;
    }


    protected Codec initCodec() {
        return null;
    }
}
